export const menudata = [
    {
        id: "1",
        label: "首页",
        path: "/Admin/Home"
    },
    {
        id: "2",
        label: "商品列表管理",
        children: [
            {
                id: "2-1",
                label: "商品列表",
                path: "/Admin/CommodityList"
            },
            {
                id: "2-2",
                label: "商品分页",
                path: "/Admin/CommodityPage"
            },
        ]
    },
    {
        id: "3",
        label: "轮播图的管理",
        children: [
            {
                id: "3-1",
                label: "轮播图列表",
                path: "/Admin/BannerList",
            },
            {
                id: "3-2",
                label: "轮播图添加",
                path: "/Admin/BannerAdd"
            }
        ]
    },
    {
        id: "4",
        label: "操作人员管理",
        children: [
            {
                id: "4-1",
                label: "操作人员列表",
                path: "/Admin/OperatorList"
            },
            {
                id: "4-2",
                label: "添加操作人员",
                path: "/Admin/OperatorAdd"
            }
        ]
    },
    {
        id: "5",
        label: "地址列表管理",
        children: [
            {
                id: "5-1",
                label: "地址对应列表",
                path: "/Admin/Adress"
            }
        ]
    },
    {
        id: "6",
        label: "商品用户管理",
        children: [
            {
                id: "6-1",
                label: "商品数量列表",
                path: "/Admin/AllStatistic"
            },
            {
                id: "6-2",
                label: "用户数量列表",
                path: "/Admin/Alluser"
            }
        ]
    },
    {
        id: "7",
        label: "图表统计管理",
        children: [
            {
                id: "7-1",
                label: "折线图",
                path: "/Admin/Echartsone"
            },
            {
                id: "7-2",
                label: "柱状图",
                path: "/Admin/Echartstwo"
            }
        ]
    },
];


//根据当前用户的权限，产生相应的菜单列表
export default function getmenudata() {
    //1.获取该用户的权限，登录时保存的权限
    let checkedKeys: string[] = [];
    let currKeys = sessionStorage.getItem('grants');
    if (currKeys) {
        checkedKeys = JSON.parse(currKeys);
    }
    if(checkedKeys.length===0){
        checkedKeys = ['1','2','2-1','2-2','3','3-1','3-2','4','4-1','4-2','5','5-1','6','6-1','6-2','7','7-1','7-2'];
    }
    //2.根据该用户的权限结合全部菜单数据，产生该用户的菜单
    interface IMenuItem {
        id: string,
        label: string,
        path?: string,
        children?: IMenuItem[]
    }
    let currMenuData: IMenuItem[] = [];  // 存储当前用户的菜单项
    menudata.forEach(item => {
        if (checkedKeys.indexOf(item.id) != -1) {
            currMenuData.push({ ...item });
            //处理子集菜单
            if (item.children) {
                //给当前菜单最后一项（新添加的项目）的children赋值为空值
                currMenuData[currMenuData.length - 1].children = [];
                item.children.forEach(itemone => {
                    if (checkedKeys.indexOf(itemone.id) != -1) {
                        currMenuData[currMenuData.length - 1].children?.push({ ...itemone })
                    }
                })

            }
        }
    })
    return currMenuData;
}